<!DOCTYPE html>
<html>
	<head>
		<include file="public:meta" />
		<link rel="stylesheet" href="../public/css/members.css">
	</head>
	<body>
        <include file="public:header" />
        <div class="split-block"></div>
        <form id="registerForm" method="post">
            <div class="ptitle border"><div class="th1">公司信息</div></div>
            <div class="list-group">
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">公司名称</div>
                    <div class="describe font12"><input type="text" name="companyname" id="companyname" placeholder="请输入公司全称"></div>
                    <div class="clear"></div>
                </div>
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">联系人</div>
                    <div class="describe font12"><input type="text" name="contact" id="contact" placeholder="请输入公司联系人"></div>
                    <div class="clear"></div>
                </div>
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">手机号</div>
                    <div class="describe font12"><input type="text" name="telephone" id="telephone" placeholder="请输入手机号" value="{$Think.get.mobile|default=''}"></div>
                    <div class="clear"></div>
                </div>
                <div class="list_height plist-txt notarrow had-remind-box qs-center link_blue" onclick="javascript:location.href='{:U('Members/login_mobile')}'">
                    <a href="javascript:;"><span class="mal"></span><span class="txt-o-red">点击立即登录</span></a>
                </div>
                <div class="list_height plist-txt">
                    <div class="pic"></div>
                    <div class="tit font14">公司座机</div>
                    <a href="#landTel" class="describe font12 js-slide landLineText">请输入</a>
                    <div class="arrow"></div>
                    <div class="clear"></div>
                </div>
            </div>
            <div class="split-block"></div>
            <div class="ptitle border"><div class="th1">账户信息</div></div>
            <div class="list-group">
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">用户名</div>
                    <div class="describe font12"><input type="text" name="username" id="username" placeholder="请输入用户名"></div>
                    <div class="clear"></div>
                </div>
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">邮箱</div>
                    <div class="describe font12"><input type="text" name="email" id="email" placeholder="请输入联系邮箱"></div>
                    <div class="clear"></div>
                </div>
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">密码</div>
                    <div class="describe font12"><input type="password" name="emailpassword" id="emailpassword" placeholder="请输入6-16位密码"></div>
                    <div class="clear"></div>
                </div>
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">确认密码</div>
                    <div class="describe font12"><input type="password" name="emailpasswordVerify" id="emailpasswordVerify" placeholder="请重复输入密码"></div>
                    <div class="clear"></div>
                </div>
            </div>
            <div class="split-block"></div>
            <div class="btn-spacing"><a id="btnRegCompany" href="javascript:;" class="qs-btn qs-btn-blue font18">注册企业会员</a></div>
            <input name="utype" type="hidden" value="{$utype}">
            <input type="hidden" name="reg_type" value="2">
            <input type="hidden" name="landline_tel" id="landline_tel" value="">
            <div id="pop" style="display:none"></div>
        </form>
        <input type="hidden" id="h-landline1" value="">
        <input type="hidden" id="h-landline2" value="">
        <input type="hidden" id="h-landline3" value="">
				<include file="public:footer" />
        <include file="public:footer_min" />
        <script src="http://static.geetest.com/static/tools/gt.js"></script>
        <script src="../public/js/popWin.js"></script>
        <!--公司固话模板-->
        <script type="text/html" id="tpl-lindTel">
            <div class="headernavfixed">
                <div class="headernav font18"><div class="title">公司固话<div class="return js-back"></div><div class="rbtn"></div></div></div>
            </div>
            <div class="split-block"></div>
            <div class="list-group">
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">区号</div>
                    <div class="describe font12"><input type="text" name="landline_tel_first" id="landline_tel_first" placeholder="请输入区号"></div>
                    <div class="clear"></div>
                </div>
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">电话</div>
                    <div class="describe font12"><input type="text" name="landline_tel_next" id="landline_tel_next" placeholder="请输入电话号码"></div>
                    <div class="clear"></div>
                </div>
                <div class="list_height plist-txt notarrow">
                    <div class="pic"></div>
                    <div class="tit font14">分机</div>
                    <div class="describe font12"><input type="text" name="landline_tel_last" id="landline_tel_last" placeholder="请输入分机号码"></div>
                    <div class="clear"></div>
                </div>
            </div>
            <div class="split-block"></div>
            <div class="btn-spacing"><a id="saveLandLine" href="javascript:;" class="qs-btn qs-btn-blue font18">保存</a></div>
        </script>
	</body>
    <script>
        /**
         * 填写固话
         */
        var subTemp = $('#tpl-lindTel').html();
        $(".landLineText").click(function(){
            var $this = $(this),
                baseTxt = $this.text(),
                baseValue = $('#landline_tel').val();
            popWin.init({
                from:"right",
                html:subTemp,
                handle:function(a){
                    if ($('#h-landline1').val().length) {
                        $('#landline_tel_first').val($('#h-landline1').val());
                    }
                    if ($('#h-landline2').val().length) {
                        $('#landline_tel_next').val($('#h-landline2').val());
                    }
                    if ($('#h-landline3').val().length) {
                        $('#landline_tel_last').val($('#h-landline3').val());
                    }
                    /**
                     * 保存固话
                     */
                    $('#saveLandLine').on('click', function() {
                        var landValue1 = $.trim($('#landline_tel_first').val());
                        var landValue2 = $.trim($('#landline_tel_next').val());
                        var landValue3 = $.trim($('#landline_tel_last').val());
                        var landValue = '';
                        if (landValue1.length) {
                            if(!inputRegValiZone(landValue1)){
                                qsToast({type:2,context: '区号格式错误'});
                                return false;
                            }
                            landValue += landValue1 + '-';
                            $('#h-landline1').val(landValue1);
                        }
                        if (!landValue2.length) {
                            qsToast({type:2,context: '电话不能为空'});
                            return false;
                        }
                        if(!inputRegValiTelNext(landValue2)){
                            qsToast({type:2,context: '电话格式错误'});
                            return false;
                        }
                        landValue +=  landValue2;
                        $('#h-landline2').val(landValue2);
                        if (landValue3.length) {
                            if (!validNumber(landValue3)) {
                                qsToast({type:2,context: '分机号为数字'});
                                return false;
                            }
                            if (landValue3.length > 4) {
                                qsToast({type:2,context: '分机号1-4位数字组成'});
                                return false;
                            }
                            landValue += '-' + landValue3;
                            $('#h-landline3').val(landValue3);
                        }
                        if (landValue.length) {
                            $('#landline_tel').val(landValue);
                            $('.landLineText').text(landValue);
                        }
                        a.close();
                    });
                }
            })
        })

        // 图片验证码
        var verifyPhoto = false;

        // 图片验证码
        function verifyPhotoDialog() {
            var verifyCodePop = new QSpopout('请输入下图中的文字或字母');
            verifyCodePop.setContent([
                '<div class="dia-captcha-item">',
                '<img src="' + qscms.root + '?m=Home&c=captcha&a=captcha&t=' + (new Date()).getTime() + '" class="dia-captcha-img">',
                '<input type="text" name="captcha-solution" class="dia-captcha-solution font16" maxlength="10">',
                '</div>'
            ].join(''));
            verifyCodePop.setBtn(1);
            verifyCodePop.show();
            verifyCodePop.setClose(false);
            $('.dia-captcha-img').on('click', function() {
                $(this).attr('src', qscms.root + '?m=Home&c=captcha&a=captcha&t=' + (new Date()).getTime());
            })
            // 确定按钮事件
            verifyCodePop.getPrimaryBtn().on('click', function () {
                var currentPhotoVal = $.trim($('.dia-captcha-solution').val());
                if (currentPhotoVal.length) {
                    $.ajax({
                        url: qscms.root + '?m=Home&c=captcha&a=captchaCode',
                        cache: false,
                        async: false,
                        type: 'post',
                        dataType: 'json',
                        data: { postcaptcha: currentPhotoVal },
                        success: function(result) {
                            if (result.status) {
                                verifyCodePop.hide();
                                doAjax();
                            } else {
                                qsToast({type:2,context: '验证码输入错误'});
                                $('.dia-captcha-img').attr('src', qscms.root + '?m=Home&c=captcha&a=captcha&t=' + (new Date()).getTime());
                            }
                        }
                    });
                } else {
                    $('.dia-captcha-solution').focus();
                    qsToast({type:2,context: '请输入文字或字母'});
                }
            });
        }

        /**
         * 注册企业会员
         */
        $('#btnRegCompany').on('click',function(){
            var configVarifyReg = <if condition="C('qscms_mobile_captcha_open') eq 1 and C('qscms_wap_captcha_config.varify_reg') eq 1">1<else/>0</if>;
            var comnameValue = $.trim($('input[name="companyname"]').val());
            var contactValue = $.trim($('input[name="contact"]').val());
            var telephoneValue = $.trim($('input[name="telephone"]').val());
            var landlineValue = $.trim($('#landline_tel').val());
            var usernameValue = $.trim($('input[name="username"]').val());
            var emailValue = $.trim($('input[name="email"]').val());
            var emailpassword = $.trim($('input[name="emailpassword"]').val());
            var emailpasswordVerify = $.trim($('input[name="emailpasswordVerify"]').val());
            if(!comnameValue){
                qsToast({type:2,context: '请填写公司名称'});
                return false;
            }
            if(!remoteValid('companyname',comnameValue)){
                qsToast({type:2,context: '该公司已被注册'});
                return false;
            }
            if(!contactValue){
                qsToast({type:2,context: '请填写联系人'});
                return false;
            }
            if (telephoneValue) {
                if(!inputRegValiMobile(telephoneValue)){
                    qsToast({type:2,context: '手机号格式错误'});
                    return false;
                }
                if(!remoteValid('mobile',telephoneValue)){
                    qsToast({type:2,context: '该手机号已被注册'});
                    return false;
                }
            }
            if(!lineMobileAchoice(telephoneValue,landlineValue)){
                qsToast({type:2,context: '公司座机和手机号至少填写一项'});
                return false;
            }
            if(!usernameValue){
                qsToast({type:2,context: '请填写用户名'});
                return false;
            }
            if(!inputRegValiUsername(usernameValue)){
                qsToast({type:2,context: '用户名中英文开头6-18位，无特殊符号'});
                return false;
            }
            if(!remoteValid('username',usernameValue)){
                qsToast({type:2,context: '该用户名已被注册'});
                return false;
            }
            if(!emailValue){
                qsToast({type:2,context: '请填写邮箱'});
                return false;
            }
            if(!inputRegValiEmail(emailValue)){
                qsToast({type:2,context: '邮箱格式错误'});
                return false;
            }
            if(!remoteValid('email',emailValue)){
                qsToast({type:2,context: '该邮箱已被注册'});
                return false;
            }
            if(!emailpassword){
                qsToast({type:2,context: '请填写密码'});
                return false;
            }
            if(!inputRegValiPassword(emailpassword)){
                qsToast({type:2,context: '密码长度要求为6-16个字符'});
                return false;
            }
            if(!emailpasswordVerify){
                qsToast({type:2,context: '请填写确认密码'});
                return false;
            }
            if(!inputRegValiPassword(emailpasswordVerify)){
                qsToast({type:2,context: '密码长度要求为6-16个字符'});
                return false;
            }
            if(!inputRegValiPasswordRepeat(emailpassword,emailpasswordVerify)){
                qsToast({type:2,context: '两次输入的密码不一致'});
                return false;
            }
            if (eval(configVarifyReg)) {
                // 后台是否开启注册验证
                if (verifyPhoto) {
                    // 图片验证码
                    verifyPhotoDialog();
                } else {
                    // 极验
                    window.captchaObj.refresh();
                    $('#pop').show();
                }
            } else {
                doAjax();
            }
        });

        // 配置极验
        $.ajax({
            url: qscms.root+'?m=Mobile&c=Captcha&type=mobile&t=' + (new Date()).getTime(),
            type: 'get',
            dataType: 'json',
            success: function(config) {
                initGeetest({
                    gt: config.gt,
                    challenge: config.challenge,
                    offline: !config.success
                }, function(captchaObj) {
                    captchaObj.appendTo("#pop");
                    captchaObj.onSuccess(function() {
                        doAjax();
                    });
                    window.captchaObj = captchaObj
                });
            }
        });

        /**
         * ajax 提交注册信息
         */
        function doAjax() {
            var url = "{:U('members/register')}";
            $.ajax({
                url: url,
                type: 'POST',
                dataType: 'json',
                data: $('#registerForm').serialize(),
                success: function (data) {
                    if(data.status==1){
                        window.location.href = data.data.url;
                    }else{
                        qsToast({type:2,context: data.msg});
                    }
                }
            });
        }

        var regularMobileC = /^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|18[0-9]{9}$|17[0-9]{9}$/;
        $('input[name=telephone]').keyup(function () {
          var currentValue = $(this).val();
          if (currentValue.length >= 11) {
            if(regularMobileC.test(currentValue) && !remoteValid('mobile',currentValue)) {
              $('.had-remind-box .mal').text('手机号 ' + currentValue + ' 已注册，');
              $('.had-remind-box').show();
            } else {
              $('.had-remind-box').hide();
            }
          } else {
            $('.had-remind-box').hide();
          }
        })

        /**
         * 验证用户名等是否重复
         * @param validType 验证类型
         * @param validValue 值
         * @returns {boolean|*}
         */
        function remoteValid(validType, validValue){
            var result = false;
            $.ajax({
                url: "{:U('members/ajax_check')}",
                cache: false,
                async: false,
                type: 'post',
                dataType: 'json',
                data: { type: validType, param: validValue },
                success: function(json) {
                    if (json && json.status) {
                        result = true;
                    } else {
                        result = false;
                    }
                }
            });
            return result;
        }

        /**
         * 手机固话至少填写一项
         * @param mobileValue 手机号
         * @param landLineValue 固话号
         * @returns {boolean}
         */
        function lineMobileAchoice(mobileValue, landLineValue){
            var achoice = true;
            if (mobileValue == '' && landLineValue == '') {
                achoice = false;
            }
            return achoice;
        }

        /**
         * 验证固话->区号
         * @param value 区号
         * @returns {boolean}
         */
        function inputRegValiZone(value){
            var regularZone = /^[0-9]{3}[0-9]?$/;
            var achoice = true;
            if (value!= "" && !regularZone.test(value)) {
                achoice = false;
            }
            return achoice;
        }

        /**
         * 验证固话->电话
         * @param value 电话
         * @returns {boolean}
         */
        function inputRegValiTelNext(value){
            var regularZone = /^[0-9]{6,11}$/;
            var achoice = true;
            if (value!= "" && !regularZone.test(value)) {
                achoice = false;
            }
            return achoice;
        }

        /**
         * 验证固话->分机号
         * @param value 分机号
         * @returns {boolean}
         */
        function validNumber(value) {
            var regularNumber = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/;
            var achoice = true;
            if (value!= "" && !regularNumber.test(value)) {
                achoice = false;
            }
            return achoice;
        }

        /**
         * 验证手机号
         * @param value 手机号
         * @returns {boolean}
         */
        function inputRegValiMobile(value){
            var regularMobile = /^13[0-9]{9}$|14[0-9]{9}$|15[0-9]{9}$|18[0-9]{9}$|17[0-9]{9}$/;
            var achoice = true;
            if (value!= "" && !regularMobile.test(value)) {
                achoice = false;
            }
            return achoice;
        }

        /**
         * 验证用户名
         * @param value 用户名
         * @returns {boolean}
         */
        function inputRegValiUsername(value){
            var regularUsername = /^(?=[\u4e00-\u9fa5a-zA-Z])(?!\d+)[\u4e00-\u9fa5\w.]{6,18}$/;
            var achoice = true;
            if (!regularUsername.test(value)) {
                achoice = false;
            }
            return achoice;
        }

        /**
         * 验证邮箱
         * @param value 邮箱
         * @returns {boolean}
         */
        function inputRegValiEmail(value){
            var regularEmail = /^[_\.0-9a-zA-Z-]+[_0-9a-zA-Z-]@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,3}$/;
            var achoice = true;
            if (!regularEmail.test(value)) {
                achoice = false;
            }
            return achoice;
        }

        /**
         * 验证密码长度
         * @param value 密码
         * @returns {boolean}
         */
        function inputRegValiPassword(value){
            achoice = true;
            if(value.length>16 || value.length<6){
                achoice = false;
            }
            return achoice;
        }

        /**
         * 验证密码两次输入是否一致
         * @param password 第一次密码
         * @param passwordRepeat 第二次密码
         * @returns {boolean}
         */
        function inputRegValiPasswordRepeat(password,passwordRepeat){
            achoice = true;
            if(password!=passwordRepeat){
                achoice = false;
            }
            return achoice;
        }
    </script>
</html>